package com.itheima.api;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.mapper.SmsHomeRecommendProductMapper;
import com.itheima.pojo.SmsHomeBrand;
import com.itheima.pojo.SmsHomeRecommendProduct;
import com.itheima.vo.PageResult;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * @author lenovo
 * @description 针对表【sms_home_recommend_product(人气推荐商品表)】的数据库操作Service实现
 * @createDate 2022-10-11 14:26:18
 */
@DubboService
public class SmsHomeRecommendProductApiImpl implements SmsHomeRecommendProductApi {

    @Autowired
    private SmsHomeRecommendProductMapper recommendProductMapper;

    //添加首页推荐
    @Override
    public void recommendProductCreate(SmsHomeRecommendProduct recommendProduct) {
        recommendProductMapper.insert(recommendProduct);
    }

    //批量删除
    @Override
    public boolean deleteByIds(List<Long> idList) {
        LambdaQueryWrapper<SmsHomeRecommendProduct> lqw = new LambdaQueryWrapper<>();
        lqw.in(idList != null && idList.size() > 0, SmsHomeRecommendProduct::getId, idList);
        int count = recommendProductMapper.delete(lqw);
        return count > 0;
    }

    //分页查询推荐品牌
    @Override
    public PageResult recommendProductList(Integer pageNum, Integer pageSize, String productName, Integer recommendStatus) {
        //构造查询条件
        //分页构造器
        IPage<SmsHomeRecommendProduct> iPage = new Page<>(pageNum, pageSize);
        //条件构造器
        LambdaQueryWrapper<SmsHomeRecommendProduct> lqw = new LambdaQueryWrapper<>();
        //条件判断
        lqw.like(StrUtil.isNotBlank(productName), SmsHomeRecommendProduct::getProductName, productName)
                .eq(ObjectUtil.isNotNull(recommendStatus), SmsHomeRecommendProduct::getRecommendStatus, recommendStatus)
                .orderByAsc(SmsHomeRecommendProduct::getSort);

        //System.out.println(advertise.getEndTime());
        recommendProductMapper.selectPage(iPage, lqw);
        List<SmsHomeRecommendProduct> list = iPage.getRecords();
        Integer total = Convert.toInt(iPage.getTotal());//总条数
        Integer totalPage = Convert.toInt(iPage.getPages());//当前分页总页数
        return new PageResult(pageNum, pageSize, total, totalPage, list);
    }

    //批量修改状态
    @Override
    public boolean updateRecommendStatus(List<Long> idList, Integer recommendStatus) {
        //条件构造器
        LambdaQueryWrapper<SmsHomeRecommendProduct> lqw = new LambdaQueryWrapper<>();
        lqw.in(idList != null && idList.size() > 0, SmsHomeRecommendProduct::getId, idList);
        SmsHomeRecommendProduct recommendProduct = new SmsHomeRecommendProduct();
        recommendProduct.setRecommendStatus(recommendStatus);
        int count = recommendProductMapper.update(recommendProduct, lqw);
        return count > 0;
    }

    //根据id查询人气推荐实例
    @Override
    public SmsHomeRecommendProduct findById(Long id) {
        return recommendProductMapper.selectById(id);
    }

    //修改排序
    @Override
    public void updateSortById(SmsHomeRecommendProduct recommendProduct) {
        recommendProductMapper.updateById(recommendProduct);
    }
}




